import { describe, it, expect, beforeAll, afterAll } from "vitest"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { GET as getYears } from "./route.js"; let tmpRoot; beforeAll(async () => { tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-years-")); process.env.NAS_ROOT_PATH = tmpRoot; // tmpRoot/NL01/2024 await fs.mkdir(path.join(tmpRoot, "NL01", "2024"), { recursive: true, }); }); afterAll(async () => { await fs.rm(tmpRoot, { recursive: true, force: true }); }); describe("GET /api/branches/[branch]/years", () => { it("returns years for a valid branch", async () => { const req = new Request("http://localhost/api/branches/NL01/years"); // In Next.js 16, ctx.params is a Promise the framework would resolve. // In tests we simulate that. const ctx = { params: Promise.resolve({ branch: "NL01" }), }; const res = await getYears(req, ctx); expect(res.status).toBe(200); const body = await res.json(); expect(body).toEqual({ branch: "NL01", years: ["2024"], }); }); it("returns 400 when branch param is missing", async () => { const req = new Request("http://localhost/api/branches/UNKNOWN/years"); const ctx = { params: Promise.resolve({}), // no branch }; const res = await getYears(req, ctx); expect(res.status).toBe(400); const body = await res.json(); expect(body.error).toBe("branch Parameter fehlt"); }); it("returns 500 when NAS_ROOT_PATH is invalid", async () => { const originalRoot = process.env.NAS_ROOT_PATH; process.env.NAS_ROOT_PATH = path.join(tmpRoot, "does-not-exist"); const req = new Request("http://localhost/api/branches/NL01/years"); const ctx = { params: Promise.resolve({ branch: "NL01" }), }; const res = await getYears(req, ctx); expect(res.status).toBe(500); const body = await res.json(); expect(body.error).toContain("Fehler beim Lesen der Jahre:"); process.env.NAS_ROOT_PATH = originalRoot; }); });